import Vue from 'vue';
import VueRouter from 'vue-router';
//导入组件

Vue.use(VueRouter);
//路由规则
const routes = [
	{
		path: '/registar',
		name:'registar',
		// component: registar
		component:() => import('@/views/registar.vue')
	},
	{
		path: '/logon',
		name:'logon',
		component:() => import('@/views/logon.vue')
	},
	{
		path: '/detall',
		name:'detall',
		component:() => import('@/views/detall.vue')
	},
	{
		path: '/detall/:id',
		name:'detall',
		component:() => import('@/views/detall.vue')
	},
	{
		
		path: '/',
		name:'layout',
		component:() => import('@/views/layout.vue'),
		//二级路由
		children:[
			{
				path:'/',
				redirect:'article'
			},
			{
				path: '/article',
				name:'article',
				component:() => import('@/views/article.vue')
			},
			{
				path: '/collect',
				name:'collect',
				component:() => import('@/views/collect.vue')
			},
			{
				path: '/like',
				name:'like',
				component:() => import('@/views/like.vue')
			},
			{
				path: '/user',
				name:'user',
				component:() => import('@/views/user.vue')
			}
		]
	}
	
];

const router = new VueRouter({
	routes
});
//路由导航守卫
//to:要到那里去
//from 从那个页面跳转
//next() 正常方向（进入from页面） 如果next('/路径')拦截跳转指定页面
//一定要调用这个方法 next()
//获取token
import {getToken} from '@/utils/storage'
//白名单
const arr =['/logon','/registar']

router.beforeEach((to,from,next) =>{
	//有1、token 2、本身就去登陆页  includes()包含
	console.log(to,from)
	if(getToken() || arr.includes(to.path)){
		next()//正常跳转
	}else{
		next('/logon')
	}
}
)
export default router;
